﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Diagnostics;
using Microsoft.Xna.Framework.Content;

namespace XNADota.ContentPipeline.Terrain
{
    [Serializable()]
    [ContentSerializerRuntimeType("XNADota.Terrain.WarCraftEnvironment, XNADota")]
    public class WarCraftEnvironmentContent
    {
        private WarCraftEnvironmentContent() { }

        public WarCraftEnvironmentContent(WCLoader Loader)
        {
            Loader.ExpectTag("W3E!"); // Expected header
            Version = Loader.ReadInt32(); // Version 11
            TextureSet = (char)Loader.ReadByte(); // Felwood texture set, texture set are set in landscape
            UseCustomTexture = (Loader.ReadInt32() == 1); // use custome tileset

            TextureNames = new string[Loader.ReadInt32()];
            for (int i = 0; i < TextureNames.Length; i++)
                TextureNames[i] = Loader.ReadString(4);

            CliffNames = new string[Loader.ReadInt32()];
            for (int i = 0; i < CliffNames.Length; i++)
                CliffNames[i] = Loader.ReadString(4);

            Width = Loader.ReadInt32();
            Height = Loader.ReadInt32();

            CenterOffsetX = Loader.ReadFloat();
            CenterOffsetY = Loader.ReadFloat();

            VertexInfoContents = new VertexInfoContent[Width * Height];
            for (int x = 0; x < Width; x++)
                for (int y = 0; y < Height; y++)
                {
                    VertexInfoContents[x + y * Width] = new VertexInfoContent(Loader);
                    VertexInfoContents[x + y * Width].Position.X = x * GridSize;
                    VertexInfoContents[x + y * Width].Position.Y =
                        (VertexInfoContents[x + y * Width].Position.Y - 0x2000 + (VertexInfoContents[x + y * Width].LayerHeight - 2) * 0x0200) / 4;
                    VertexInfoContents[x + y * Width].Position.Z = y * GridSize;
                }
        }

        public const int GridSize = 128;

        public int Version;
        public char TextureSet;
        public bool UseCustomTexture;
        public string[] TextureNames;
        public string[] CliffNames;
        public int Width;
        public int Height;
        public float CenterOffsetX;
        public float CenterOffsetY;

        public VertexInfoContent[] VertexInfoContents;
    }
}
